package com.uzai.console.service.device.impl;

import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.console.dto.uzaichat.kefuclient.KefuClientQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.login.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/***
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class DeviceTypeServiceImpl implements DeviceTypeService {

    @Autowired
    private KefuClientMapper kefuClientMapper;

       /***
     * 查询设备类型下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public List<SelectKeyVo> findDeviceTypeListSelect(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //个人微信
        SelectKeyVo selectKeyVo_private = new SelectKeyVo();
        selectKeyVo_private.setKey(DeviceTypeEnum.DT_PC_WECHAT.getKey());
        selectKeyVo_private.setValue("个人微信");
        selectKeyVo_private.setIsDefault(1);
        selectKeyVoList.add(selectKeyVo_private);

        //企业微信
        SelectKeyVo selectKeyVo_work = new SelectKeyVo();
        selectKeyVo_work.setKey(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
        selectKeyVo_work.setValue("企业微信");
        selectKeyVoList.add(selectKeyVo_work);

        //公众号
        SelectKeyVo selectKeyVo_pub = new SelectKeyVo();
        selectKeyVo_pub.setKey(DeviceTypeEnum.WX_PUB.getKey());
        selectKeyVo_pub.setValue("公众号");
        selectKeyVoList.add(selectKeyVo_pub);

        //小程序
        SelectKeyVo selectKeyVo_mini = new SelectKeyVo();
        selectKeyVo_mini.setKey(DeviceTypeEnum.MINI_PRO.getKey());
        selectKeyVo_mini.setValue("小程序");
        selectKeyVoList.add(selectKeyVo_mini);

        return selectKeyVoList;
    }

    /***
     * 查询登录账号对应的设备类型下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public List<SelectKeyVo> findDeviceTypeListByLogin(LoginVo loginVo){

        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();

        //封装设备类型，去除重复的类型
        Set<String> deviceCodeSet = new HashSet<>();

        //子账号登录
        if(loginVo.getType() != null && loginVo.getType().intValue() == 1){
            //查询该子账号的id
            //验证子账号所在角色是否具有访问该接口权限
            KefuClientQuery kefuClientQuery = new KefuClientQuery();
            kefuClientQuery.setMerId(loginVo.getMerId());
            kefuClientQuery.setKefuUserId(loginVo.getAccountId());
            List<KefuClient> kefuClientList = kefuClientMapper.findByList(kefuClientQuery);
            if(kefuClientList != null){
                for (KefuClient kefuClient : kefuClientList){
                    deviceCodeSet.add(kefuClient.getDeviceType());
                }
            }

            if(deviceCodeSet != null && deviceCodeSet.size() > 0){
                for (String key : deviceCodeSet){
                    if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(key)){
                        //个人微信
                        SelectKeyVo selectKeyVo_private = new SelectKeyVo();
                        selectKeyVo_private.setKey(DeviceTypeEnum.DT_PC_WECHAT.getKey());
                        selectKeyVo_private.setValue("个人微信");
                        selectKeyVo_private.setIsDefault(1);
                        selectKeyVoList.add(selectKeyVo_private);
                    }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(key)){
                        //企业微信
                        SelectKeyVo selectKeyVo_work = new SelectKeyVo();
                        selectKeyVo_work.setKey(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
                        selectKeyVo_work.setValue("企业微信");
                        selectKeyVoList.add(selectKeyVo_work);
                    }else if(DeviceTypeEnum.WX_PUB.getKey().equals(key)){
                        //公众号
                        SelectKeyVo selectKeyVo_pub = new SelectKeyVo();
                        selectKeyVo_pub.setKey(DeviceTypeEnum.WX_PUB.getKey());
                        selectKeyVo_pub.setValue("公众号");
                        selectKeyVoList.add(selectKeyVo_pub);
                    }else if(DeviceTypeEnum.MINI_PRO.getKey().equals(key)){
                        //小程序
                        SelectKeyVo selectKeyVo_mini = new SelectKeyVo();
                        selectKeyVo_mini.setKey(DeviceTypeEnum.MINI_PRO.getKey());
                        selectKeyVo_mini.setValue("小程序");
                        selectKeyVoList.add(selectKeyVo_mini);
                    }
                }
            }
        }else{//主账号登录
            //个人微信
            SelectKeyVo selectKeyVo_private = new SelectKeyVo();
            selectKeyVo_private.setKey(DeviceTypeEnum.DT_PC_WECHAT.getKey());
            selectKeyVo_private.setValue("个人微信");
            selectKeyVo_private.setIsDefault(1);
            selectKeyVoList.add(selectKeyVo_private);

            //企业微信
            SelectKeyVo selectKeyVo_work = new SelectKeyVo();
            selectKeyVo_work.setKey(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
            selectKeyVo_work.setValue("企业微信");
            selectKeyVoList.add(selectKeyVo_work);

            //公众号
            SelectKeyVo selectKeyVo_pub = new SelectKeyVo();
            selectKeyVo_pub.setKey(DeviceTypeEnum.WX_PUB.getKey());
            selectKeyVo_pub.setValue("公众号");
            selectKeyVoList.add(selectKeyVo_pub);
        }

        return selectKeyVoList;
    }

}
